from flask_restful import Resource, reqparse, request

# from src.api.mobile_predict_api import process_mobile
from src.api.laptop_predict_api import process_laptop
# from src.api.tablet_predict_api import process_tablet
from src.utils.config import logger
from src.utils.feishu_message import feishu_messager
import traceback


parser = reqparse.RequestParser()
parser.add_argument("category", type=int, required=True)
parser.add_argument("data", type=dict, action="append", default=None)
# 重新加载模型命令
parser.add_argument("reload", type=int, default=0)


VALID_CATEGORY = { 5}


class PricePredictorAPI(Resource):
    """
    价格预测接口
    """

    def post(self,name):
        args = parser.parse_args()
        access_ip = request.remote_addr
        category = args['category']
        data = args['data']
        reload = args['reload']

        logger.info('access ip @{} category@{}'.format(access_ip,category))
        if category not in VALID_CATEGORY:
            return {'code': 1001, 'message': 'category@{} is not valid'.format(category), 'results': []}, 200
        if reload == 0 and not data:
            return {'code': 1002, 'message': 'data is empty', 'results': []}, 200

        logger.info('received data@{}'.format(data))
        logger.info('batch size@{}'.format(len(data)))
        try:
            # if category == 1:
            #     result = process_mobile(data, reload)
            #     return result, 200
            if category == 5:
                result = process_laptop(data, reload)
                return result, 200
            # if category == 6:
            #     result = process_tablet(data, reload)
            #     return result, 200

            return {'code': 1003, 'message': 'category price model is not complete', 'results': []}, 200
        except Exception as e:
            message = traceback.format_exc()
            logger.critical('异常定位：{}'.format(message))
            feishu_messager.send_message('API内部服务异常：{}'.format(e))
            return {'code': -1, 'message': '{}'.format(e), 'results': []}, 500


class PricePredictorAPI_old(Resource):
    """
    价格预测接口
    """

    def post(self):
        args = parser.parse_args()
        access_ip = request.remote_addr
        category = args['category']
        data = args['data']
        reload = args['reload']

        logger.info('access ip @{}'.format(access_ip))
        if category not in VALID_CATEGORY:
            return {'code': 1001, 'message': 'category@{} is not valid'.format(category), 'results': []}, 200
        if reload == 0 and not data:
            return {'code': 1002, 'message': 'data is empty', 'results': []}, 200

        logger.info('received data@{}'.format(data))
        logger.info('batch size@{}'.format(len(data)))
        try:
            # if category == 1:
            #     result = process_mobile(data, reload)
            #     return result, 200
            if category == 5:
                result = process_laptop(data, reload)
                return result, 200
            # if category == 6:
            #     result = process_tablet(data, reload)
            #     return result, 200

            return {'code': 1003, 'message': 'category price model is not complete', 'results': []}, 200
        except Exception as e:
            logger.critical('API内部服务异常：{}'.format(e))
            # raise TypeError('API内部服务异常:') from e
            feishu_messager.send_message('API内部服务异常：{}'.format(e))
            return {'code': -1, 'message': '{}'.format(e), 'results': []}, 500
